package com.dj.mingkong.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.util.Random;

public class BaseEoUtil {
    private static Logger logger = LoggerFactory.getLogger(BaseEoUtil.class);
    private static String workerId = null;
    private static final Random RANDOM = new Random();

    public BaseEoUtil() {
    }

    public static Long getWorkerId() {
        if (StringUtils.isNotEmpty(workerId)) {
            return Long.valueOf(workerId);
        } else {
            try {
                if (StringUtils.isEmpty(workerId)) {
                    InetAddress localHost = InetAddress.getLocalHost();
                    String hostAddress = localHost.getHostAddress();
                    int lastIndex = hostAddress.lastIndexOf(".") + 1;
                    String subHostAddress = hostAddress.substring(0, lastIndex - 1);
                    int lastisecondndex = subHostAddress.lastIndexOf(".") + 1;
                    workerId = String.valueOf(Integer.parseInt(subHostAddress.substring(lastisecondndex, subHostAddress.length())) + Integer.parseInt(hostAddress.substring(lastIndex, hostAddress.length())));
                }
            } catch (Exception var8) {
                logger.error("error ip generate app.worker.id.", var8);
            } finally {
                if (StringUtils.isEmpty(workerId)) {
                    logger.warn("use random to generate generate app.worker.id");
                    workerId = String.valueOf(RANDOM.nextInt(1023));
                }

            }

            return Long.valueOf(workerId);
        }
    }

    public static void setWorkerId(String workerId) {
        BaseEoUtil.workerId = workerId;
    }

    private static boolean sqlValidate(String sqlParams, String badStr) {
        boolean bool = true;
        if (StringUtils.isNotEmpty(sqlParams)) {
            String sqlLowerCase = sqlParams.toLowerCase();
            String[] badStrs = badStr.split("\\|");

            for (int i = 0; i < badStrs.length; ++i) {
                if (sqlLowerCase.contains(badStrs[i])) {
                    return false;
                }
            }
        }

        return bool;
    }
}
